﻿using System;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace Aspects.Panels
{
    public partial class SoundBar : Control
    {
        private int _amplitude = 0;
        private int _maxValue = 0;

        public SoundBar()
        {
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint |
                ControlStyles.OptimizedDoubleBuffer, true);
            InitializeComponent();
        }
    
        /// <summary>
        /// Current Value
        /// </summary>
        public int Amplitude
        {
            get { return _amplitude; }
            set
            {
                _amplitude = value;
                MaxAmplitude = value;
                this.Invalidate();
            }
        }

        public int MaxAmplitude
        {
            get
            {
                return _maxValue;
            }
            set
            {
                _maxValue = Math.Max(value, _maxValue);
            }

        }

        /// <summary>
        /// Paints the volume meter
        /// </summary>
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);

            pe.Graphics.DrawRectangle(Pens.Black, 0, 0, this.Width - 1, this.Height - 1);
            //the rectangle's largest sound will be 32768. So i need to scale that according to the width.

            int width = GetWidthByPercentage();
            int height = this.Height - 2;

            pe.Graphics.FillRectangle(new SolidBrush(ForeColor), 1, 1, width, height);
        }

        private int GetWidthByPercentage()
        {
            float percentage = (float)Amplitude/ 32768.0f;
            
            return (int)((Width-2)*percentage);
        }

    }
}
